<?php


class AdminController extends Zend_Controller_Action
{

    public function init()
    {
      /* Initialize action controller here */

      $auth = Zend_Auth::getInstance();
      if ($auth->hasIdentity()) {
          // Identity exists; get it
          $this->identity = $auth->getIdentity();
          //echo  $identity;
      }else{
      	$this->_redirect('/auth/');
      }

    }

    public function indexAction()
    {
        $this->view->title = "Galleries update";
        $this->view->headTitle($this->view->title);

        $galleries = new Application_Model_Galleries();
        $this->view->galleries = $galleries->fetchAll();

    }

    public function addAction()
    {
        $this->view->title = "Add new gallery";
        $this->view->headTitle($this->view->title);

        $form = new Application_Form_Gallery();
        $form->submit->setLabel('Add');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $title = $form->getValue('title');

                $adapter = new Zend_File_Transfer_Adapter_Http();
				$adapter->setDestination('.\upload');
				$est = split('[.]', $adapter->getFileName());
				$est =  '.'.$est[sizeof($est)-1];
				$namefile = implode('_', array((int)$this->identity->user_id, date('YmdHis'), $est));
				$adapter->addFilter('Rename', $namefile);

				if (!$adapter->receive()) {
				    $messages = $adapter->getMessages();
				    echo implode("\n", $messages);
				}

                $msg = $form->getValue('msg');
                $galleries = new Application_Model_Galleries();
                $galleries->addGallery($title, $msg, $namefile);
                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        }

    }

    public function editAction()
    {
        $this->view->title = "Edit Gallery";
        $this->view->headTitle($this->view->title);

        $form = new Application_Form_Gallery();
        $form->submit->setLabel('Save');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int)$form->getValue('id');
                $title = $form->getValue('title');
                $msg = $form->getValue('msg');

                $adapter = new Zend_File_Transfer_Adapter_Http();

				$adapter->setDestination('.\upload');

				if (!$adapter->receive()) {
				    $messages = $adapter->getMessages();
				    echo implode("\n", $messages);

				    $galleries = new Application_Model_Galleries();
                	$galleries->update2Gallery($id, $title, $msg);

				}else{
					$est = split('[.]', $adapter->getFileName());

					$est =  '.'.$est[sizeof($est)-1];
					$namefile = implode('_', array((int)$this->identity->user_id, date('YmdHis'), $est));
					$adapter->addFilter('Rename', $namefile);

					$galleries = new Application_Model_Galleries();
	                $galleries->updateGallery($id, $title, $msg, $namefile);

				}

				$this->_helper->redirector('index');


//                $galleries = new Application_Model_Galleries();
//                $galleries->updateGallery($id, $title, $msg, $namefile);
//                echo $title .' -- '. $namefile;

            } else {
                $form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $galleries = new Application_Model_Galleries();
                $gallery = $galleries->getGallery($id);
                $this->view->gallery = $gallery;
                $form->populate($gallery);
            }
        }
    }

    public function deleteAction()
    {
        $this->view->title = "Delete gallery";
        $this->view->headTitle($this->view->title);

        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                $id = $this->getRequest()->getPost('id');
                $galleries = new Application_Model_Galleries();
                $galleries->deleteGallery($id);
            }
            $this->_helper->redirector('index');
        } else {
            $id = $this->_getParam('id', 0);
            $galleries = new Application_Model_Galleries();
            $this->view->gallery = $galleries->getGallery($id);
        }
    }
}



